//
//  LGDoctorCodeView.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/5/20.
//  医生二维码

#import "LGDoctorCodeView.h"
#import "CIImage+SGExtension.h"
@interface LGDoctorCodeView()
/**   */
@property (nonatomic , strong)UIImageView *baseView;
/**   */
@property (nonatomic , strong)UIImageView *codeView;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UILabel *postLabel;

@end

@implementation LGDoctorCodeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        [UIView animateWithDuration:0.3 animations:^{
            self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.7];
            self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 1000, 1000);
        }];
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenViewAnimation)]];
    }
    return self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSDictionary *codeDic = @{@"id":LGNSString(dataDic[@"id"]),@"tenant_id":LGNSString(dataDic[@"tenantId"])};
    [self createGenerateQRCode:[ObjectTool objectToJson:codeDic]];
    self.nameLabel.text = LGNSString(dataDic[@"realName"]);
    self.postLabel.text = LGNSString(dataDic[@"postName"]);
}

// 生成二维码
- (void)createGenerateQRCode:(NSString *)content{
    
    // 1、创建滤镜对象
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 恢复滤镜的默认属性
    [filter setDefaults];
    
    //2 将字符串转换成
    NSData *infoData = [content dataUsingEncoding:NSUTF8StringEncoding];
    
    // 通过KVC设置滤镜inputMessage数据
    [filter setValue:infoData forKeyPath:@"inputMessage"];
    
    // 3、获得滤镜输出的图像
    CIImage *outputImage = [filter outputImage];

    // 5、将CIImage转换成UIImage，并放大显示
    CGFloat imageViewW = self.codeView.bounds.size.width;
    self.codeView.image = [outputImage SG_createNonInterpolatedWithSize:imageViewW];
}

-(void)hiddenViewAnimation{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 0.001, 0.001);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    
    self.baseView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"二维码-背景"]];
    self.baseView.frame = CGRectMake((Screen_W-viewPix(300))/2.0, (Screen_H-viewPix(336))/2.0, viewPix(300), viewPix(336));
    self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 0.001, 0.001);
    [self addSubview:self.baseView];
    
    self.codeView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(22), viewPix(21), viewPix(256), viewPix(256))];
    [self.baseView addSubview:self.codeView];
    
    self.nameLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
    self.nameLabel.frame = CGRectMake(viewPix(22), viewPix(275), viewPix(256), viewPix(25));
    [self.baseView addSubview:self.nameLabel];
    
    self.postLabel = [UILabel lableWithText:@"" colorString:@"#968F8F" textFont:LGFont(16) textAlignment:NSTextAlignmentCenter lines:1];
    self.postLabel.frame = CGRectMake(viewPix(22), viewPix(302), viewPix(256), viewPix(22));
    [self.baseView addSubview:self.postLabel];
    
}


@end
